#include <func.h>

int mkdirServer(int netfd, const char *username, MYSQL *db)
{
    //接收绝对路径
    int datalen;
    recvn(netfd, &datalen, sizeof(datalen));
    char *absPath = calloc(datalen, sizeof(char));
    recvn(netfd, absPath, datalen);
    //去数据库中寻找有没有这个路径
    //分离出目录名和要创建处的路径名
    char *dirName = basename(absPath);
    char *pathName = dirname(absPath);
    MYSQL_ROW row = findFile(pathName, username, db);
    int failFlag = 0;
    if (row == NULL)
    {
        failFlag = 1;
        printf("mkdir: %s:%s not found!\n", username, pathName);
    }
    int ret = send(netfd, &failFlag, sizeof(failFlag), MSG_NOSIGNAL);
    ERROR_CHECK(ret, -1, "send find route of server in database failFlag in mkdirServer");
    if (failFlag != 0)
    {
        free(absPath);
        return -1;
    }
    //创建目录项
    char sql[256];
    sprintf(sql, "insert filesystem (filename, preid, type, username, isdelete) VALUES ('%s', %d, '%c', '%s', %d)", dirName, atoi(row[0]), 'd', username, 0);
    int ret = mysql_query(db, sql);
    MYSQL_ERROR_CHECK(ret, -1, "mysql_query in mkdirServer", db);

    free(absPath);
    return 0;
}